﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using SuperMarketManagement.DAO.NHibernate;
using SuperMarketManagement.Entity;

namespace SuperMarketManagement.Web.Controllers
{
    public class SampleController : Controller
    {
        //
        // GET: /Sample/

        public ActionResult Install()
        {
            using (DAOFactory fac = new DAOFactory())
            {

                //create sample invoice


                //create sample roles
                Role[] roles = {
                                   new Role("admin"),
                                   new Role("inventory-manager"),
                                   new Role("sale-manager"),
                                   new Role("sale"),
                                   new Role("customer-manager")
                               };

                fac.RoleDAO.Save(roles);

                //create sample employees
                Employee[] employees = new Employee[]
                                           {
                                               new Employee("admin", "Administrator", Sex.MALE,
                                                            Convert.ToDateTime("1986/9/26"), "123456", true, roles[0]),
                                               new Employee("inventory-manager", "Inventory Manager", Sex.FEMALE,
                                                            Convert.ToDateTime("1989/10/22"), "123456", true, roles[1]),
                                               new Employee("sale-manager", "Sale Manager", Sex.FEMALE,
                                                            Convert.ToDateTime("1989/10/22"), "123456", true, roles[2]),
                                               new Employee("sale-person", "Sale Person", Sex.FEMALE,
                                                            Convert.ToDateTime("1989/10/22"), "123456", true, roles[2]),
                                               new Employee("customer-manager", "Customer Manager", Sex.FEMALE,
                                                            Convert.ToDateTime("1988/3/14"), "123456", true, roles[2],
                                                            roles[3]),
                                               new Employee("phuongnd08", "Nguyen Duc Phuong", Sex.MALE,
                                                            Convert.ToDateTime("1990/3/14"), "123456", false, roles[2],
                                                            roles[3])
                                           };

                fac.EmployeeDAO.Save(employees);

                //create sample customers

                Customer[] customers = new Customer[]
                                           {
                                               new Customer("Tran Tung", "01-01", 1, "091000111",
                                                            Convert.ToDateTime("1986/9/26"), true, "123456"),
                                               new Customer("Nguyen Thao", "01-02", 1, "12344567",
                                                            Convert.ToDateTime("1998/6/19"), true, "123456")
                                           };

                fac.CustomerDAO.Save(customers);


                //create sample categories
                Category beer, milk, washing, vegetables, fruit, meat;
                Category root = new Category("root",
                                             new Category("Foods",
                                                          vegetables = new Category("Vegetables"),
                                                          fruit = new Category("Fruit"),
                                                          meat = new Category("Meat")
                                                 ),
                                             new Category("Drinks",
                                                          milk = new Category("Milk"),
                                                          beer = new Category("Beer")
                                                 ),
                                             washing = new Category("Washing"),
                                             new Category("Tooth brush"),
                                             new Category("Soap")
                    );

                fac.CategoryDAO.Save(root);

                //create sample products;
                Product[] products = {
                                         new Product("Rau muong", "RAU-1", "Dalat", "Mot dang cap rau xanh", "Bundle",
                                                     2000, vegetables,
                                                     new ProductBlock(1000, DateTime.Now, 50, 30),
                                                     new ProductBlock(1200, DateTime.Now, 40, 40)),
                                         new Product("Xa lach", "RAU-2", "Dalat", "Rau xanh cho moi nha", "Bó",
                                                     2500, vegetables,
                                                     new ProductBlock(1000, DateTime.Now, 50, 30),
                                                     new ProductBlock(1200, DateTime.Now, 40, 40)),
                                         new Product("Carot", "RAU-3", "Dalat", "Giau carotene", "Củ",
                                                     2400, vegetables,
                                                     new ProductBlock(1000, DateTime.Now, 50, 30),
                                                     new ProductBlock(1200, DateTime.Now, 40, 40)),
                                        new Product("Khoai tay", "RAU-3", "Dalat", "Thuc pham dinh duong cho moi nguoi", "Củ",
                                                     5000, vegetables,
                                                     new ProductBlock(1000, DateTime.Now, 50, 30),
                                                     new ProductBlock(1200, DateTime.Now, 40, 40)),
                                         new Product("Xoai gion", "FRU-1", "Camranh, Khanhhoa", "Xoai so zach", "Fruit",
                                                     18000, fruit,
                                                     new ProductBlock(3000, DateTime.Now, 100, 10),
                                                     new ProductBlock(3300, DateTime.Now, 100, 100)),
                                         new Product("Chôm chôm", "FRU-2", "Miền Tây", "Ai chôm không?", "Fruit",
                                                     20000, fruit,
                                                     new ProductBlock(3000, DateTime.Now, 100, 10),
                                                     new ProductBlock(3300, DateTime.Now, 100, 100)),
                                         new Product("Măng cụt", "FRU-1", "Lái thêu", "Trái cây cho VIP", "Fruit",
                                                     50000, fruit,
                                                     new ProductBlock(3000, DateTime.Now, 100, 10),
                                                     new ProductBlock(3300, DateTime.Now, 100, 100)),
                                         new Product("Thịt bò", "MEAT-1", "Long Thành",
                                                     "Rất nhiều Cholesterol", "Kg", 240000, meat,
                                                     new ProductBlock(200000, DateTime.Now, 100, 40),
                                                     new ProductBlock(210000, DateTime.Now, 50, 50)),
                                         new Product("Thịt heo", "MEAT-2", "Bavifo", "Dành cho người còm",
                                                     "Kg", 225000, meat,
                                                     new ProductBlock(192000, DateTime.Now, 50, 30),
                                                     new ProductBlock(196000, DateTime.Now, 40, 40)),
                                         new Product("Thịt gà", "MEAT-3", "Bavifo", "Thơm xương ngọt thịt",
                                                     "Kg", 225000, meat,
                                                     new ProductBlock(192000, DateTime.Now, 50, 30),
                                                     new ProductBlock(196000, DateTime.Now, 40, 40)),
                                         new Product("Sua chua Vinamilk", "YOG-1", "Vinamilk", "", "Boxes", 13000L, milk,
                                                     new ProductBlock(9000, DateTime.Now, 500, 145),
                                                     new ProductBlock(9200, DateTime.Now, 400, 400)),
                                         new Product("Sua tuoi Lothamilk", "FM-1", "Lothamilk", "", "Bottle", 6000L,
                                                     milk,
                                                     new ProductBlock(4000, DateTime.Now, 300, 100),
                                                     new ProductBlock(4200, DateTime.Now, 200, 200))
                                     };
                products[0].AddShrinkage(new Shrinkage(10, 1000, DateTime.Now));

                fac.ProductDAO.Save(products);
                //create sample invoices
                Invoice[] invoices = {
                                         new Invoice(customers[0], DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0)),
                                                     new InvoiceItem(products[0], 3, products[0].CurrentSellingPrice),
                                                     new InvoiceItem(products[1], 1, products[1].CurrentSellingPrice)),
                                         new Invoice(customers[0], DateTime.Now,
                                                     new InvoiceItem(products[2], 2, products[2].CurrentSellingPrice),
                                                     new InvoiceItem(products[3], 2, products[3].CurrentSellingPrice))
                                     };

                fac.InvoiceDAO.Save(invoices);
                // create sample Sale program

                SaleProgram[] salePrograms = {
                                                 new SaleProgram(Convert.ToDateTime("2009/12/13"),
                                                                 Convert.ToDateTime("2010/01/01"), 1000000, 0.01),
                                                 new SaleProgram(Convert.ToDateTime("2009/12/13"),
                                                                 Convert.ToDateTime("2010/01/01"), 5000000, 0.02),
                                                 new SaleProgram(Convert.ToDateTime("2009/12/13"),
                                                                 Convert.ToDateTime("2010/01/01"), 10000000, 0.05),
                                             };
                fac.SaleProgramDAO.Save(salePrograms);
            }
            return View();
        }

    }
}
